package org.groupg.practice.guiPractice.awt;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class GanttChartExample extends JFrame {

    private final GanttPanel ganttPanel;

    public GanttChartExample() {
        ganttPanel = new GanttPanel();
        add(ganttPanel);
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GanttChartExample example = new GanttChartExample();
            example.setVisible(true);
        });
    }

    private static class Task {
        String name;
        int start;
        int end;

        public Task(String name, int start, int end) {
            this.name = name;
            this.start = start;
            this.end = end;
        }
    }

    private static class GanttPanel extends JPanel {

        private final List<Task> tasks = new ArrayList<>();
        private final int rowHeight = 50;
        private final int barPadding = 10;
        private final int timelineStart = 50; // 开始时间轴的位置
        private final int timelineUnitWidth = 50; // 每个时间单位的宽度
        private final int maxTimeUnit = 15; // 最大时间单位数，用于确定时间轴的长度

        public GanttPanel() {
            tasks.add(new Task("Task 1", 1, 5));
            tasks.add(new Task("Task 2", 3, 8));
            tasks.add(new Task("Task 3", 6, 10));
            // Add more tasks as needed
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            drawTimeline(g); // 绘制时间轴
            drawTasks(g); // 绘制任务条
        }

        private void drawTimeline(Graphics g) {
            g.setColor(Color.BLACK);
            int x = timelineStart;
            for (int i = 0; i <= maxTimeUnit; i++) {
                // 绘制刻度线
                g.drawLine(x, barPadding, x, getHeight() - barPadding);
                // 绘制刻度标签
                String label = String.valueOf(i);
                FontMetrics fm = g.getFontMetrics();
                int labelX = x - fm.stringWidth(label) / 2;
                int labelY = getHeight() - barPadding + fm.getAscent();
                g.drawString(label, labelX, labelY);
                // 移动到下一个时间单位
                x += timelineUnitWidth;
            }
        }

        private void drawTasks(Graphics g) {
            int y = barPadding;
            for (Task task : tasks) {
                // 绘制任务名
                g.drawString(task.name, barPadding, y + (rowHeight / 2) + (g.getFontMetrics().getHeight() / 2));
                // 绘制任务条
                g.setColor(Color.BLUE);
                int taskX = timelineStart + task.start * timelineUnitWidth;
                int taskWidth = (task.end - task.start) * timelineUnitWidth;
                g.fillRect(taskX, y, taskWidth, rowHeight - barPadding * 2);
                g.setColor(Color.BLACK);
                g.drawRect(taskX, y, taskWidth, rowHeight - barPadding * 2);
                // 移动到下一行
                y += rowHeight;
            }
        }
    }
}